import 'package:async/async.dart';
import 'package:boss/models/commodity.dart';
import 'package:boss/models/store_menu.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/page/commodity/pickerCategories/picker_commodity_provider.dart';
import 'package:boss/ui/widget/center_title_app_bar.dart';
import 'package:boss/ui/widget/loader/loader.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';
import 'package:provider/provider.dart';
import 'package:router/annotation.dart';

import 'widget/categories_browser.dart';

@RouterConfig(PickerCategoriesPage.pageName)
class PickerCategoriesPage extends StatefulWidget {
  static const pageName = '/pickerCategoriesPage';

  @Autowired()
  final int type; // 0选择  1数量

  @Autowired()
  final bool isShowPackage;

  @Autowired()
  final bool isShowSku;

  @Autowired()
  final List<Commodity> selectedCommodities;

  @Autowired()
  final List<int> filterType;

  const PickerCategoriesPage({
    this.type,
    this.selectedCommodities,
    this.isShowPackage,
    this.filterType,
    this.isShowSku,
  });

  @override
  _PickerCategoriesPageState createState() => _PickerCategoriesPageState('菜品分类');
}

class _PickerCategoriesPageState extends BaseState<PickerCategoriesPage> {
  _PickerCategoriesPageState(String pageName) : super(pageName);

  Future<Result<List<StoreMenu>>> _loadTask(BuildContext context) async {
    final commodityProvider = Provider.of<PickerCommodityProvider>(context, listen: false);

    return commodityProvider.initData(
      selectedCommoditys: widget.selectedCommodities,
      isShowPackage: widget.isShowPackage,
      isShowSku: widget.isShowSku,
      filterType: widget.filterType,
    );
  }

  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => PickerCommodityProvider(widget.type, widget.isShowSku, widget.isShowPackage),
      child: Builder(
        builder: (context) {
          return Scaffold(
            appBar: CenterTitleAppBar(
              title: Consumer<PickerCommodityProvider>(
                builder: (context, commodityProvider, child) {
                  return RichText(
                    text: TextSpan(
                      text: '全部菜品',
                      style: TextStyleConstant().normal_1_36,
                      children: [
                        TextSpan(
                          text: '(${commodityProvider.allCommodityCount}个)',
                          style: TextStyleConstant().normal_2_26,
                        ),
                      ],
                    ),
                  );
                },
              ),
            ),
            body: Loader(
                loadTask: () => _loadTask(context),
                builder: (context) {
                  return CategoriesBrowser();
                }),
          );
        },
      ),
    );
  }
}
